﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;

namespace BaseSet.Entity.Common.DataOperation
{
    /// <summary>
    /// 编解码
    /// </summary>
    public class Codec
    {
        /// <summary>
        /// 将指定的值，用指定的编码进行编码
        /// </summary>
        /// <param name="Value">要编码的字符串</param>
        /// <param name="Charset">编码方式</param>
        /// <returns>编码后的字符串</returns>
        public static string Decode(string Value, string Charset)
        {
            if (string.IsNullOrEmpty(Value)) return Value;
            if (string.IsNullOrEmpty(Charset))
            {
                Charset = Encoding.Default.BodyName;
            }
            byte[] bytes = Convert.FromBase64String(Value);
            return Encoding.GetEncoding(Charset).GetString(bytes);
        }

        /// <summary>
        ///用指定的编码进行编码，如果没有指定则用可与邮件代理正文标记一起使用的当前编码对字符串进行编码
        /// </summary>
        /// <param name="Value">要编码的字符串</param>
        /// <param name="Charset">编码方式</param>
        /// <returns>编码后的字符串</returns>
        public static string Encode(string Value, string Charset)
        {
            if (string.IsNullOrEmpty(Value)) return Value;
            if (string.IsNullOrEmpty(Charset))
            {
                Charset = Encoding.Default.BodyName;
            }
            return Convert.ToBase64String(Encoding.GetEncoding(Charset).GetBytes(Value));
        }

        /// <summary>
        /// 将字符串由utf8编码转换成gb2312编码
        /// </summary>
        /// <param name="utf8_str">utf8编码编码的字符串</param>
        /// <returns>gb2312的字符串</returns>
        public static string UTF8ToGB2312(string utf8_str)
        {
            string utfinfo = utf8_str;
            string gb2312info = string.Empty;

            Encoding utf8 = Encoding.UTF8;
            Encoding gb2312 = Encoding.GetEncoding("gb2312");

            byte[] unicodeBytes = utf8.GetBytes(utfinfo);
            byte[] asciiBytes = Encoding.Convert(utf8, gb2312, unicodeBytes);

            char[] asciiChars = new char[gb2312.GetCharCount(asciiBytes, 0, asciiBytes.Length)];
            gb2312.GetChars(asciiBytes, 0, asciiBytes.Length, asciiChars, 0);
            gb2312info = new string(asciiChars);

            return gb2312info;
        }

        /// <summary>
        /// 将字符串转为Unicode编码
        /// </summary>
        /// <param name="strChar">下载文件内容</param>
        /// <returns>转换后的字符串</returns>
        public static string StringToUnicode(string input)
        {
            Encoding bigEndianUnicode = Encoding.BigEndianUnicode;
            char[] chArray = input.ToCharArray();
            string str = "";
            foreach (char ch in chArray)
            {
                if (ch.Equals('\r') || ch.Equals('\n'))
                {
                    str = str + ch;
                }
                else
                {
                    byte[] bytes = bigEndianUnicode.GetBytes(new char[] { ch });
                    str = (str + @"\u") + string.Format("{0:X2}", bytes[0]) + string.Format("{0:X2}", bytes[1]);
                }
            }
            return str;
        }

        /// <summary>
        /// Url编码
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string UrlEncode(string str)
        {
            return HttpUtility.UrlEncode(str);
        }

        /// <summary>
        /// Url解码
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string UrlDecode(string str)
        {
            return HttpUtility.UrlDecode(str);
        }
    }
}
